package model

import (
	"database/sql/driver"
	"encoding/json"
	"gorm.io/plugin/soft_delete"
	"time"
)

type BaseModel struct {
	ID        uint64                `gorm:"primarykey;comment:主键"`
	CreateAt  time.Time             `gorm:"column:create_time;autoCreateTime;comment:创建时间"`
	UpdateAt  time.Time             `gorm:"column:update_time;autoUpdateTime;comment:更新时间"`
	IsDeleted soft_delete.DeletedAt `gorm:"type:int(1);not null;softDelete:flag,DeletedAtField:DeletedAt;default:0;comment:0未删除1删除"`
	Status    int32                 `gorm:"type:int(1);not null;default:1;comment:0未发布1发布"`
	// 忽略该字段，- 表示无读写，-:migration 表示无迁移权限，-:all 表示无读写迁移权限
	//Children any `gorm:"-" json:"children"`
}

// 商品库存表
type GoodsStocks struct {
	BaseModel
	GoodsId uint64 `gorm:"type:bigint;not null;default:0;index;comment:商品ID"`
	Stocks  int32  `gorm:"type:int;not null;default:0;comment:商品库存"`
	Version int32  `gorm:"type:bigint;not null;default:0;comment:乐观锁的标识"`
}

func (c *GoodsStocks) TableName() string {
	return "ksd_goods_stocks"
}

type GoodsDetail struct {
	GoodsId uint64
	Num     int32
}
type GoodsDetailList []GoodsDetail

func (g GoodsDetailList) Value() (driver.Value, error) {
	return json.Marshal(g)
}

// 实现 sql.Scanner 接口，Scan 将 value 扫描至 Jsonb
func (g *GoodsDetailList) Scan(value interface{}) error {
	return json.Unmarshal(value.([]byte), &g)
}

type StockSellDetail struct {
	OrderNo string          `gorm:"type:varchar(200);index:idx_order_sn,unique;"`
	Status  int32           `gorm:"type:varchar(200)"` //1 表示已扣减 2. 表示已归还
	Detail  GoodsDetailList `gorm:"type:varchar(1000)"`
}

func (StockSellDetail) TableName() string {
	return "ksd_stock_selldetail"
}
